<?php
class CCommon
{
	static function get_stock_api($p_strSku, $p_strCountry)
	{
		$strApi	= null;
		switch(strtolower($p_strCountry))
		{
			case 'vi':
				$strApi	= 'https://lazada-vn-s.herokuapp.com/api/get_skus_qt?api_key=12b9dc15eaa7e1d0947451489ad15f0b';
				break;
			case 'th':
				$strApi	= 'https://lazada-th-s.herokuapp.com/api/get_skus_qt?api_key=12b9dc15eaa7e1d0947451489ad15f0b';
				break;
			default :
				break;
		}
		$arrStock	= array();
		if($strApi)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $strApi);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, 'skus='. $p_strSku);
			$store = curl_exec ($ch);
			
			$arrJson	= $store ? json_decode($store, true) : null;
			$arrStock	= isset($arrJson['result']) ? $arrJson['result'] : null;
		}
		return $arrStock;
	}
	
	static function get_info_sku($p_strSku, $p_strCountry)
	{
		$arrSku		= explode(',', $p_strSku);
		$objWms		= new QWms($p_strCountry);
		$arrResult	= $objWms->wms_info($arrSku);
		$arrData	= isset($arrResult['for_sale']) ? $arrResult['for_sale'] : null;
		
		$arrStock		= array();
		$arrForSale		= array();
		$arrInTransit	= array();
		#For Sale
		if($arrData)
		{
			foreach($arrData as $data)
			{
				$arrForSale[$data['sku']] = $data['count'];
			}
		}
		
		#Transit
		$arrData	= isset($arrResult['in_transit']) ? $arrResult['in_transit'] : null;
		if($arrData)
		{
			foreach($arrData as $data)
			{
				$arrInTransit[$data['sku']]	= $data['quantity'];
			}
		}
		
		foreach($arrSku as $sku)
		{
			$arrStock[$sku]	= array(
				'For Sale'		=> isset($arrForSale[$sku]) ? intval($arrForSale[$sku]) : 0,
				'In Transit'	=> isset($arrInTransit[$sku]) ? intval($arrInTransit[$sku]) : 0
			);
		}
		return $arrStock;
	}
	
	static function exists_table($p_strTable)
	{
		$arrTable	= QCommon::get_all_table();
		foreach($arrTable as $data){
			foreach($data as $key=>$table){
				if($table==$p_strTable){
					return true;
				}
			}
		}
		return false;
	}
	
	static function mkTimeDateTo($p_strDate)
	{
		if (!$p_strDate) return 0;
		$strResult 	= trim($p_strDate);
		$arrTemp 	= explode(' ',$strResult);
		$arrInfo	= explode('-', $arrTemp[0]);
		return mktime(23,59,59,	$arrInfo[1], $arrInfo[2], $arrInfo[0]);
	}
	
	static function mkTimeDateFrom($p_strDate)
	{
		if (!$p_strDate) return 0;
		$strResult 	= trim($p_strDate);
		$arrTemp 	= explode(' ',$strResult);
		$arrInfo	= explode('-', $arrTemp[0]);
		return mktime(0,0,0,	$arrInfo[1], $arrInfo[2], $arrInfo[0]);
	}
	
	static function mkTimeDate($p_strDate) // follow mysql format yyyy-mm-dd H:i:s
	{
		if (!$p_strDate) return 0;
		$strResult 	= trim($p_strDate);
		$arrInfo 	= explode(' ',	$strResult);
		$arrDate	= explode('-', 	$arrInfo[0]);
		$arrTime	= array();
		if( isset($arrInfo[1]) )
		{
			$arrTime	= explode(':', 	$arrInfo[1]);
		}
		$intHour	= isset($arrTime[0]) ? $arrTime[0] : 0;
		$intMinute	= isset($arrTime[1]) ? $arrTime[1] : 0;
		$intSecond	= isset($arrTime[2]) ? $arrTime[2] : 0;
		return mktime($intHour, $intMinute, $intSecond, intval($arrDate[1]), intval($arrDate[2]), intval($arrDate[0]));
	}
	
	static function format_column_name($p_strColumn)
	{
		return str_replace(' ', '_', strtolower(trim($p_strColumn)));
	}
	
	static function send_mail($p_strSubject, $p_strBody, $p_strMailTo)
	{
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'To: '.$p_strMailTo. "\r\n";
		$headers .= 'From: System DragonTool <system-dragontool@lazada.vn>' . "\r\n";
		@mail($p_strMailTo, $p_strSubject, $p_strBody, $headers);
	}
	
	static function clear_chart_cache($strCountry)
	{
		$strPath	= ROOT_SITE.'/static/chart/' . $strCountry;
		$arrFiles	= scandir($strPath);
		if($arrFiles)
		{
			foreach($arrFiles as $file){
				if( $file!='.' && $file!='..' ){
					@unlink($strPath.'/'.$file);
				}
			}
		}
	}
}
?>